// stores/user.js
import { defineStore } from 'pinia';
import api from '@/api/api';
import { MENU_AUTH } from "@/constants/stationConfig"


export const useUserStore = defineStore('user', {
  state: () => ({
    userInfo: {},
    menuData: null,
    menuBtnData: [], // 用户按钮权限列表
  }),
  actions: {
    async fetchUserData(id) {
      try {
        const params = {
          userId: id,
        }
        const res = await api.queryUserInfo(params);
        if (res && res.code === 0) {
          this.userInfo = res.data;
          this.menuBtnData = res.data.permissions;
          return res.data;
        }
      } catch (error) {
        console.error('error:', error);
      }
    },
    logout() {

      this.userInfo = {};
      this.menuBtnData = [];
      this.menuData = null;
    },
    async fetchMenuData(ids) {
      try {
        const params = {
          menuType: MENU_AUTH.menu,
          roleIds: ids.length > 0
            ? ids.join(",")
            : "",
        }
        const res = await api.baseMenuList(params);
        if (res && res.code === 0) {
          this.menuData = res.data;

          return res.data;
        }
      } catch (error) {
        console.error('error:', error);
      }
    },

  }
});